from fastapi.exceptions import RequestValidationError,HTTPException
from fastapi.responses import JSONResponse
from .log import getLogger
import os
from fastapi import Request,status


logger = getLogger(os.environ.get('APP_NAME'))
# 全局异常(逻辑错误)
async def global_exception_handler(request, exc):
    logger.error(f'错误：{exc.detail}')

    return JSONResponse({
        'code': exc.status_code,
        'err_msg': exc.detail,
        'status': 'global_error'
    })

#全局请求校验异常（参数错误）
async def global_requests_exception(request, exc):
    return JSONResponse({
        'code': status.HTTP_400_BAD_REQUEST,
        'err_msg': exc.errors()[0],
        'status': 'requests_error'
    })